package Indexserver;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;


import Bean.SourceBean;
import Indexserver.Utils.Constant;
import Indexserver.Utils.DbAccess;
import Indexserver.Utils.StringUtil;
import Indexserver.Utils.XmlReplace;

public class WriteNewsCacheSource{
	public ArrayList<SourceBean> getSource(){
		Indexserver.Utils.DbAccess db =  new DbAccess();
		try{
			db.connectTemp("localhost", Constant.DBINDEX);
			String sql = "SELECT * FROM tnews_source";
			db.stmt = db.conn.prepareStatement(sql);
			db.rs = db.stmt.executeQuery();
			ArrayList<SourceBean> bean = new ArrayList<SourceBean>();
			while(db.rs.next()){
				String sourceId = db.rs.getString("sourceID");
				String name = db.rs.getString("name");
				System.out.println(name);
				SourceBean sbean = new SourceBean();
				sbean.setSourceID(sourceId);
				sbean.setSourceName(name);
				bean.add(sbean);
			}
			return bean;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			if(db!=null) db.DBClose();
		}
	}
	
	public void write(){
		DbAccess db =  new DbAccess();
		try{
			db.connectTemp("localhost", Constant.DBINDEX);
			String sql = "SELECT * FROM tdocumentinfo WHERE date > ? AND sourceID=? LIMIT 15";
			Calendar now = Calendar.getInstance();
			now.add(Calendar.HOUR_OF_DAY,-10);
			String date = now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-"+now.get(Calendar.DATE)+" 00:00:00";
			ArrayList<SourceBean> sbean = getSource();
			String xml = "";
			for(int i=0; i < sbean.size(); i++){
				System.out.println("Dang lay: "+sbean.get(i).getSourceName());
				
				db.stmt = db.conn.prepareStatement(sql);
				db.stmt.setString(1, date);
				db.stmt.setString(2, sbean.get(i).getSourceID());
				
				System.out.println(sbean.get(i).getSourceID()+" "+date);
				
				db.rs = db.stmt.executeQuery();
				xml = "";
				while(db.rs.next()){
					String id = db.rs.getString("dID");
					String sum = Indexserver.Utils.Utils.getSummary(id).trim();
					xml += "<news lid=\""+id+"\" " +
							"id=\""+id+"\" " +
							"t=\""+XmlReplace.escape(db.rs.getString("title"))+"\" " +
							"s=\""+XmlReplace.escape(sbean.get(i).getSourceName())+"\" " +
							"l=\""+XmlReplace.escape(db.rs.getString("url"))+"\" " +
							"c=\"\" " +
							"sm=\""+XmlReplace.escape(sum)+"\" " +
							"d=\""+StringUtil.convertDate(db.rs.getString("date"), "vn")+"\" " +
							"i=\"http://index.vietica.com/thumbnail/DoProc?l="+
							URLEncoder.encode(db.rs.getString("date"),"UTF-8")+"&t=/large/"+"\" " +
							"cid=\""+db.rs.getString("catid")+"\"/>\n";
				}
				System.out.println("Lay Xong: "+sbean.get(i).getSourceName()+"\n");
				Indexserver.Utils.FileUtils.writeFile(xml, Constant.DATAPATH+"cat_"+sbean.get(i).getSourceID(), "UTF-8");
				db.stmt.close();
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(db!=null) db.DBClose();
		}
	}
	
	public void writeTemp(){
		DbAccess db =  new DbAccess();
		try{
			db.connectTemp("localhost", Constant.DBINDEX);
			String sql = " select max(date) as a from tdocumentinfo where sourceID =?";
			ArrayList<SourceBean> sbean = getSource();
			String xml = "";
			for(int i=0; i < sbean.size(); i++){
				db.stmt = db.conn.prepareStatement(sql);
				db.stmt.setString(1, sbean.get(i).getSourceID());
				System.out.println(sql);
				db.rs = db.stmt.executeQuery();
				while(db.rs.next()){
					xml += sbean.get(i).getSourceID()+" "+ sbean.get(i).getSourceName()+" "+db.rs.getString("a")+"\n";
				}
				System.out.println("Lay Xong: "+sbean.get(i).getSourceName()+"\n");
				db.stmt.close();
			}
			Indexserver.Utils.FileUtils.writeFile(xml, Constant.DATAPATH+"temp.txt", "UTF-8");			
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(db!=null) db.DBClose();
		}
	}
	
	
	
	public static void main(String[] args) {
		new WriteNewsCacheSource().writeTemp();
	}
	
}